import 'package:citiesocial/_include.dart';

class HotCatalogue extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _HotCatalogueState();
}

class _HotCatalogueState extends BaseState<HotCatalogue> {
  final _list = List<CategoryEntity>();

  void _onCategoryTap(int index) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => GoodsByCatalogue(_list[index]),
      ),
    );
  }

  void _loadData() async {
    LoadingDialog.show(context);
    HttpManager.hotCategory(context: context).then((categories) {
      LoadingDialog.dismiss(context);
      _list.clear();
      if (categories != null && categories.length > 0) {
        _list.addAll(categories);
        setState(() {});
      }
    });
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.withCart(context, title: '热门分类'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: DListView(
            onRefresh: () async {
              _loadData();
            },
            itemCount: _list.length,
            itemBuilder: (_, index) => ItemMenu(
                  _list[index].cat_name,
                  leftIcon: _list[index].cat_icon,
                  leftIconEdge: 100.0,
                  rightIcon: 'imgs/js_tabs_profile_img_more_g.png',
                  onTap: () => _onCategoryTap(index),
                ),
          ),
        ),
      );
}
